<template>
	<view class="content">
		<view class="pathList">
			<view class="pathList-item" :class="{ active: item.checked }" v-for="item in pathAttractionList"
				:key="item.id">
				<checkbox-group @change="goswitch(item)">
					<checkbox :value="item.id.toString()" :checked="item.checked" />
				</checkbox-group>
				<view class="pathAttractionList">
					<image @click="getImage(item.images)"
						:src="item.images ? $u.fileBaseUrl + item.images.split(',')[0] :'../../../static/images/index/zwtp.png' "
						mode="widthFix">
					</image>
					<view>
						<text>{{item.name}}</text>
						<text>{{item.description}}</text>
					</view>
					<view v-if="item.checked" class="order-number">{{item.order}}</view>
				</view>
			</view>
		</view>
		<view class="customizedRoute">
			<button class="customizedRoute-button" @click="goNextStep">下一步</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pathAttractionList: [],
				page: {
					pageNum: 1,
					pageSize: 10
				},
				total: 0,
				selectedCount: 0,
			};
		},
		computed: {
			sortedPathAttractionList() {
				return [...this.pathAttractionList].sort((a, b) => {
					if (a.checked && b.checked) {
						return a.order - b.order;
					}
					if (a.checked) return -1;
					if (b.checked) return 1;
					return 0;
				});
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '加载中...'
			})
			this.getAttractionList()
			setTimeout(() => {
				uni.hideLoading()
			}, 1000)
		},
		onPullDownRefresh() {
			this.page.pageNum = 1
			this.page.pageSize = 10
			this.getAttractionList()
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			this.loadMore();
		},
		methods: {
			getImage(images) {
				uni.previewImage({
					current: this.$u.fileBaseUrl + images,
					urls: [this.$u.fileBaseUrl + images]
				})
			},
			async getAttractionList() {
				let res = await this.$u.api.getAttractionList(this.page)
				if (res.code == 200) {
					this.pathAttractionList = res.rows.map(item => ({
						...item,
						checked: false,
						order: null
					}));
					this.total = res.total
				}
			},

			goswitch(item) {
				if (!item.checked) {
					// 如果是新选中的项目
					this.lastOrder++;
					item.order = this.lastOrder;
					this.selectedCount++;
				} else {
					// 如果是取消选中的项目
					item.order = null;
					this.selectedCount--;
				}
				item.checked = !item.checked;
				this.recalculateOrder();
			},

			recalculateOrder() {
				// 按照原始顺序排序选中的项目
				const sortedSelectedItems = this.pathAttractionList
					.filter(item => item.checked)
					.sort((a, b) => a.order - b.order);

				// 重新分配序号
				sortedSelectedItems.forEach((item, index) => {
					item.order = index + 1;
				});

				this.lastOrder = sortedSelectedItems.length;
			},

			goNextStep() {
				const orderPathAttractionList = this.pathAttractionList
					.filter(item => item.checked)
					.map(item => ({
						attractionId: item.id,
						sort: item.order
					}));

				const orderPathAttractionListStr = JSON.stringify(orderPathAttractionList);
				const encodedOrderList = encodeURIComponent(orderPathAttractionListStr);
				console.log(this.selectedCount);
				if (this.selectedCount <= 0) {
					this.$u.toast('请选择景点')
					return
				}
				uni.navigateTo({
					url: `/packagesIndex/pages/photography/photography?orderPathAttractionList=${encodedOrderList}`
				});

			},
			loadMore() {
				setTimeout(() => {
					if (this.pathAttractionList.length >= this.total) {
						uni.showToast({
							title: '没有更多数据了',
							icon: "none"
						})
					} else {
						this.page.pageNum = this.page.pageNum + 1;
						this.page.pageSize = this.page.pageSize + 10;
						uni.showLoading({
							title: '加载中...'
						})
						this.getAttractionList()
						setTimeout(() => {
							uni.hideLoading()
						}, 2000)
					}
				}, 1000);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #fff;
		min-height: 95vh;
		padding-bottom: 50rpx;

		.pathList {
			margin: 0 20rpx;

			.pathList-item {
				padding: 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;

				&:last-child {
					margin-bottom: 100rpx;
				}

				.pathAttractionList {
					width: 96%;
					display: flex;
					border-radius: 20rpx;
					box-shadow: 0rpx 7rpx 20rpx #ccc;
					padding: 20rpx;
					box-sizing: border-box;
					margin-left: 10rpx;
					position: relative;

					// &.active {
					// 	background: #F3F8FF;
					// 	z-index: 99999;
					// 	border: 1rpx solid #9cdcfe;
					// 	border-radius: 20rpx;
					// }

					image {
						width: 300rpx;
						margin: 20rpx;
						border-radius: 20rpx;
					}

					view {
						background-color: #fff;
						margin: 20rpx;

						text {
							width: 250rpx;
							display: block;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;

							&:nth-child(1) {
								font-weight: 700;
							}
						}


					}



					.order-number {
						position: absolute;
						top: 10rpx;
						right: 10rpx;
						background-color: $overall-color;
						color: white;
						border-radius: 50%;
						width: 40rpx;
						height: 40rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						font-size: 24rpx;
					}
				}


			}
		}

		.customizedRoute {
			padding: 30rpx 0 50rpx 0;
			width: 720rpx;
			position: fixed;
			bottom: 0;
			background-color: #fff;
			margin: 0 15rpx;

			.customizedRoute-button {
				width: 100%;
				color: #fff;
				background-color: $overall-color;
				text-align: center;
				padding: 20rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
			}
		}
	}
</style>